package com.infinit.easyreader.adpter;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * 类：com.infinit.easyreader.adpter.RecommendAdapter V[1.0.0]
 * 作用：云页面上方的viewpager的轮播的适配器
 * Created by rzy on 2016/6/28.
 */
public class RecommendAdapter extends PagerAdapter implements View.OnClickListener {

    private List<View> views;
    private OnItemClickListener onItemClickListener;

    public RecommendAdapter(List<View> views) {
        this.views = views;
        if (views == null) {
            this.views = new ArrayList<>();
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setViews(List<View> views) {
        if (views != null) {
            this.views = views;
        }
    }

    @Override
    public int getCount() {
        return views.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = views.get(position);
        view.setTag(position + "");
        //设置监听器
        view.setOnClickListener(this);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(views.get(position));
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void onClick(View v) {
        onItemClickListener.onItemClick(Integer.parseInt((String) v.getTag()), v);
    }

    public interface OnItemClickListener {
        void onItemClick(int position, View view);
    }
}
